#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"

trigger-builder-herokuish-pre-build-buildpack() {
  declare desc="builder-herokuish pre-build-buildpack plugin trigger"
  declare trigger="pre-build-buildpack"
  declare APP="$1"
  local IMAGE CID

  local DOCKER_COMMIT_LABEL_ARGS=("--change" "LABEL org.label-schema.schema-version=1.0" "--change" "LABEL org.label-schema.vendor=dokku" "--change" "LABEL com.dokku.app-name=$APP")
  local DOCKER_RUN_LABEL_ARGS="--label=com.dokku.app-name=$APP"

  verify_app_name "$APP"
  IMAGE=$(get_app_image_name "$APP")

  [[ -z $(config_get --global CURL_CONNECT_TIMEOUT) ]] && config_set --global CURL_CONNECT_TIMEOUT=90
  [[ -z $(config_get --global CURL_TIMEOUT) ]] && config_set --global CURL_TIMEOUT=600

  dokku_log_info1 "Adding BUILD_ENV to build environment..."
  # create build env files for use in buildpacks like this:
  # https://github.com/niteoweb/heroku-buildpack-buildout/blob/5879fa3418f7d8e079f1aa5816ba1adde73f4948/bin/compile#L34
  CID=$(config_bundle --merged "$APP" | "$DOCKER_BIN" container run "${DOCKER_RUN_LABEL_ARGS[@]}" $DOKKU_GLOBAL_RUN_ARGS -i -a stdin "$IMAGE" /bin/bash -c "mkdir -p /tmp/env; cat | tar -x -C /tmp/env")
  if test "$("$DOCKER_BIN" container wait "$CID")" -ne 0; then
    dokku_log_warn "Failure injecting BUILD_ENV into build environment"
    return 1
  fi

  "$DOCKER_BIN" container commit "${DOCKER_COMMIT_LABEL_ARGS[@]}" "$CID" "$IMAGE" >/dev/null

  # create build env for 'old style' buildpacks and dokku plugins
  CID=$(config_export app "$APP" --format envfile --merged | "$DOCKER_BIN" container run "${DOCKER_RUN_LABEL_ARGS[@]}" $DOKKU_GLOBAL_RUN_ARGS -i -a stdin "$IMAGE" /bin/bash -c "cat >> /app/.env")
  if test "$("$DOCKER_BIN" container wait "$CID")" -ne 0; then
    dokku_log_warn "Failure injecting old-style BUILD_ENV"
    return 1
  fi

  "$DOCKER_BIN" container commit "${DOCKER_COMMIT_LABEL_ARGS[@]}" "$CID" "$IMAGE" >/dev/null
}

trigger-builder-herokuish-pre-build-buildpack "$@"
